package o;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.huawei.android.util.HwNotchSizeUtil;
import com.huawei.appgallery.assistantdock.buoydock.card.bean.BuoyLanternCardBeanBuoy;
import com.huawei.gamebox.plugin.gameservice.bean.GameInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import o.dex.d;
import org.json.JSONException;
import org.json.JSONObject;

public class bgd {
    private static bgd ˋ;

    public static synchronized bgd ॱ() {
        bgd o_bgd;
        synchronized (bgd.class) {
            if (ˋ == null) {
                ˋ = new bgd();
            }
            o_bgd = ˋ;
        }
        return o_bgd;
    }

    public d ˊ(Context context) {
        if (context == null || TextUtils.isEmpty(ˋ())) {
            return d.NORMAL;
        }
        Object obj = null;
        try {
            obj = new JSONObject(eua.ˏ().ॱ("dockMode", "")).getString(ˋ());
        } catch (JSONException e) {
            Logger.ˋ("BuoyDockSupport", "get lastShowMode failed, meet json exception");
        }
        Logger.ˎ("BuoyDockSupport", "packageName:" + ˋ() + ",lastShowMode:" + obj);
        if (TextUtils.isEmpty(obj)) {
            if (ˏ()) {
                return d.NORMAL;
            }
            return d.DOCK;
        } else if (obj.equals(d.DOCK.ˏ())) {
            return d.DOCK;
        } else {
            return d.NORMAL;
        }
    }

    private boolean ˏ() {
        fae ˏ = fam.ॱ().ˏ();
        if (ˏ == null) {
            return false;
        }
        GameInfo gameInfo = ˏ.getGameInfo();
        if (gameInfo == null) {
            return false;
        }
        ezo ˋ = ezs.ˏ().ˋ(gameInfo);
        if (ˋ == null) {
            return false;
        }
        BuoyLanternCardBeanBuoy ͺ = ˋ.ͺ();
        if (ͺ == null) {
            return false;
        }
        List ˊ = ͺ.ˊ();
        if (ˊ == null || ˊ.isEmpty()) {
            return false;
        }
        return true;
    }

    public void ॱ(d dVar) {
        if (dVar != null && dVar != d.GUIDE) {
            Object ॱ = eua.ˏ().ॱ("dockMode", "");
            try {
                JSONObject jSONObject;
                if (TextUtils.isEmpty(ॱ)) {
                    jSONObject = new JSONObject();
                } else {
                    JSONObject jSONObject2 = new JSONObject(ॱ);
                    if (jSONObject2.length() >= 100) {
                        jSONObject2.remove((String) jSONObject2.names().get(0));
                    }
                    jSONObject = jSONObject2;
                }
                jSONObject.put(ˋ(), dVar.ˏ());
                eua.ˏ().ˋ("dockMode", jSONObject.toString());
            } catch (JSONException e) {
                Logger.e("BuoyDockSupport", "saveBuoyShowType meet JSONException");
            }
        }
    }

    public void ˏ(Context context) {
        if (context != null) {
            int ॱ = eua.ˏ().ॱ("slideCount", 0) + 1;
            eua.ˏ().ˊ("slideCount", ॱ);
            if (ॱ == 3) {
                new Handler(Looper.getMainLooper()).post(new d(context));
            }
        }
    }

    public void ॱ(Context context, fad o_fad) {
        new Handler(Looper.getMainLooper()).post(new a(context, o_fad));
    }

    private String ˋ() {
        fae ˏ = fam.ॱ().ˏ();
        if (ˏ == null || ˏ.getGameInfo() == null) {
            return "";
        }
        return ˏ.getGameInfo().getPackageName();
    }

    public boolean ˎ(Context context, String str) {
        if (context == null) {
            return false;
        }
        try {
            if (!HwNotchSizeUtil.hasNotchInScreen()) {
                return false;
            }
            int ˎ = ˎ(str);
            if (ˎ == 1) {
                Logger.ˋ("BuoyDockSupport", "set app:" + str + " show notch area by setting");
                return true;
            } else if (ˎ == 2) {
                Logger.ˋ("BuoyDockSupport", "set app:" + str + " hide notch area by setting");
                return false;
            } else {
                try {
                    Bundle bundle = context.getPackageManager().getApplicationInfo(str, DnsConfig.MAX_CACHE_ENTRIES).metaData;
                    if (bundle == null) {
                        return false;
                    }
                    boolean z = bundle.getBoolean("android.notch_support");
                    Logger.ˎ("BuoyDockSupport", "packageName:" + str + ",android.notch_support:" + z);
                    return z;
                } catch (Exception e) {
                    Logger.e("BuoyDockSupport", "get android.notch_support data error:");
                    return false;
                }
            }
        } catch (Throwable th) {
            Logger.e("BuoyDockSupport", "get has NotchInScreen error");
            return false;
        }
    }

    public boolean ˋ(String str) {
        try {
            Class cls = Class.forName("com.huawei.android.app.PackageManagerEx");
            if (1 == ((Integer) cls.getDeclaredMethod("getAppUseSideMode", new Class[]{String.class}).invoke(cls, new Object[]{str})).intValue()) {
                return true;
            }
            return false;
        } catch (NoSuchMethodException e) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet NoSuchMethodException");
            return false;
        } catch (IllegalAccessException e2) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet IllegalAccessException");
            return false;
        } catch (InvocationTargetException e3) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet InvocationTargetException");
            return false;
        } catch (ClassNotFoundException e4) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet ClassNotFoundException");
            return false;
        }
    }

    private int ˎ(String str) {
        try {
            Class cls = Class.forName("com.huawei.android.app.PackageManagerEx");
            return ((Integer) cls.getDeclaredMethod("getAppUseNotchMode", new Class[]{String.class}).invoke(cls, new Object[]{str})).intValue();
        } catch (NoSuchMethodException e) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet NoSuchMethodException");
            return 0;
        } catch (IllegalAccessException e2) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet IllegalAccessException");
            return 0;
        } catch (InvocationTargetException e3) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet InvocationTargetException");
            return 0;
        } catch (ClassNotFoundException e4) {
            Logger.e("BuoyDockSupport", " PackageManagerEx.getAppUseNotchMode meet ClassNotFoundException");
            return 0;
        }
    }
}
